$(function () {
    $("#jqGrid").Grid({
        url: '../sale/insurancecompany/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '提成方式', name: 'type', index: 'type', hidden: true},
			{label: '保险公司', name: 'name', index: 'name',align: 'center', width: 80},
			{label: '保险公司返点', name: 'profit', index: 'profit',align: 'center', width: 80},
            {label: '提成方式', name: 'typeStr', index: 'type', width: 80,align: 'center',
                formatter:function(cellvalue, options, rows) {
                    if (rows.type==1) {
                        return '<span class="label label-primary">保险利润</span>';
                    } else if (rows.type==2) {
                        return '<span class="label label-success">全额百分比</span>';
                    } else if (rows.type == 3) {
                        return "固定金额"
                    }
                }
            },
			{label: '员工提成', name: 'poundage', align: 'center',index: 'poundage', width: 80}]
    });
});

var vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		baseInsuranceCompany: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: ''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.baseInsuranceCompany = {type:1};
		},
		update: function (event) {
            var id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
		},
		saveOrUpdate: function (event) {
            var url = vm.baseInsuranceCompany.id == null ? "../sale/insurancecompany/save" : "../sale/insurancecompany/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.baseInsuranceCompany),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            var ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../sale/insurancecompany/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../sale/insurancecompany/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.baseInsuranceCompany = r.baseInsuranceCompany;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: 1
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});